<template>

    <div class="pagination">
        <div>start:{{ startAndEnd.start }} - end:{{ startAndEnd.end }}- 每页：{{ pageSize }}条- 当前页{{ pageNo }}</div><br />
        <button :disabled="pageNo == 1" @click="$emit('getPageIndex', pageNo - 1)">上一页</button>
        <button v-if="startAndEnd.start != 1" @click="$emit('getPageIndex', 1)">1</button>
        <button v-if="startAndEnd.start > 2">···</button>

        <button v-for="(num, index) in startAndEnd.end" :key="index" v-if="startAndEnd.start <= num"
            @click="$emit('getPageIndex', num)" :class="{ active: num == pageNo }">{{ num }}</button>


        <button v-if="(totalPage - startAndEnd.end) > 1">···</button>
        <button v-if="startAndEnd.end != totalPage" @click="$emit('getPageIndex', totalPage)">{{ totalPage }}</button>
        <button :disabled="pageNo == totalPage" @click="$emit('getPageIndex', pageNo + 1)">下一页</button>

        <button style="margin-left: 30px">共 {{ total }} 条</button>
    </div>
</template>

<script>
export default {
    name: "Pagination",
    props: ['pageNo', 'pageSize', 'total', 'continuous'],   // <Pagination :pageNo="1" :pageSize="7" :total="91" :continuous="5" />
    computed: {
        totalPage() {
            return Math.ceil(this.total / this.pageSize);
        },
        middleVal() {
            return parseInt(this.continuous / 2)
        },
        startAndEnd() {
            let start = 0, end = 0;
            const { totalPage, continuous, pageNo, middleVal } = this;
            if (continuous > totalPage) {
                start = 1;
                end = totalPage;
            } else {
                start = pageNo - middleVal;
                end = pageNo + middleVal;
            }

            if (start < 1) {
                start = 1;
                end = continuous;
            }

            if (end > totalPage) {
                end = totalPage;
                start = totalPage - continuous;
            }
            return { start, end }
        },
    },

}
</script>

<style lang="less" scoped>
.pagination {
    text-align: center;

    button {
        margin: 0 5px;
        background-color: #f4f4f5;
        color: #606266;
        outline: none;
        border-radius: 2px;
        padding: 0 4px;
        vertical-align: top;
        display: inline-block;
        font-size: 13px;
        min-width: 35.5px;
        height: 28px;
        line-height: 28px;
        cursor: pointer;
        box-sizing: border-box;
        text-align: center;
        border: 0;

        &[disabled] {
            color: #c0c4cc;
            cursor: not-allowed;
        }

        &.active {
            cursor: not-allowed;
            background-color: #409eff;
            color: #fff;
        }
    }
}
</style>